package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import building.InfoAgent;

/**
 * Implement the mouse event to create manually an environment configuration
 * 
 */
public class ClickHandler extends MouseAdapter {
	int i, j;
	InfoAgent info;
	SimpleAboutDialog dialog;

	/**
	 * @param label
	 *            label that identifies a button
	 * @param i
	 *            coordinate X of the mouse
	 * @param j
	 *            coordinate Y of the mouse
	 * @param infoAgent
	 */
	ClickHandler(int i, int j) {
		this.i = i;
		this.j = j;
		this.info = null;

	}

	public ClickHandler(int x, int y, InfoAgent agent) {
		this.i = x;
		this.j = y;
		this.info = agent;
		// System.err.println("ClickHandler " + info);

	}

	@Override
	/**
	 * Choose and set the status of the square to clean/dirty/obstacle at mouse click
	 */
	public void mouseClicked(MouseEvent e) {
		super.mouseClicked(e);

		if (e.getButton() == MouseEvent.BUTTON1) {

			// System.err.println("BUTTON X " + e.getX() + " Y " + e.getY());
			dialog = new SimpleAboutDialog(new JFrame(), info);
			dialog.setVisible(true);
			dialog.setAlwaysOnTop(true);
			/*
			 * JDialog d = new JDialog(); d.setSize(400, 300);
			 * d.setLocationRelativeTo(null); d.setLocation(e.getX(), e.getY());
			 * d.setVisible(true); d.setTitle("Infoagent"); JFrame frame = new
			 * JFrame(); frame.setTitle("Informazioni"); frame.add(new
			 * JLabel(info)); System.err.println("INFO " + info); d.add(frame);
			 */

			/*
			 * assert the agent is not on this square if (GridPanel.env.agent.x
			 * == i && GridPanel.env.agent.y == j) return; make this square of
			 * the current type GridPanel.env.floor.set (i, j,
			 * GridPanel.currType); label.setIcon (GridPanel.currIcon);
			 * 
			 * } else if (e.getButton() == MouseEvent.BUTTON3) { show a popup
			 * menu to choose what to insert JPopupMenu menu = new JPopupMenu();
			 * JMenuItem dirty = new JMenuItem("Dirty");
			 * dirty.addActionListener(new ActionListener() {
			 * 
			 * @Override public void actionPerformed(ActionEvent arg0) {
			 * GridPanel.currType = Square.Type.DIRTY; GridPanel.currIcon =
			 * GridPanel.dirtIcon; } }); JMenuItem clean = new
			 * JMenuItem("Clean"); clean.addActionListener(new ActionListener()
			 * {
			 * 
			 * @Override public void actionPerformed(ActionEvent arg0) {
			 * GridPanel.currType = Square.Type.CLEAN; GridPanel.currIcon =
			 * GridPanel.tileIcon; } }); JMenuItem obstacle = new
			 * JMenuItem("Obstacle"); obstacle.addActionListener(new
			 * ActionListener() {
			 * 
			 * @Override public void actionPerformed(ActionEvent arg0) {
			 * GridPanel.currType = Square.Type.OBSTACLE; GridPanel.currIcon =
			 * GridPanel.obstacleIcon; } });
			 */

		}
	}

	public class SimpleAboutDialog extends JDialog {
		/**
        *
        */
		private static final long serialVersionUID = 1L;
		private InfoAgent info;

		public SimpleAboutDialog(JFrame parent, InfoAgent agent) {
			super(parent, "About Dialog", true);
			this.info = agent;
			/*
			 * String f = "Age Range : "; if(info.U18) f+="u18 ";
			 * if(info.A19_30) f+="a19_30 "; if(info.A31_79) f+="a31_79 ";
			 * if(info.O80) f+="o80 "; f += "\n";
			 */
			final JLabel age_range = new JLabel(info.getAge().toString());

			String mob = "mobilit� ridotta : ";
			if (agent.reduceMob == 1)
				mob += "s� \n";
			else
				mob += "no \n";

			final JLabel mob_r = new JLabel(mob);

			/*
			 * String sex = "male : "; if(agent.male ==1 ) sex+="s� \n"; else
			 * sex+="no \n";
			 */

			final JLabel gender = new JLabel(info.getGender().toString());

			String healt = "healt : " + (int) agent.healthy + "\n";

			final JLabel healt_l = new JLabel(healt);

			String panic = "panic : ";
			if (agent.panic == 0)
				panic += "no \n";
			if (agent.panic == 40)
				panic += "confusional state \n";
			if (agent.panic == 80)
				panic += "yes \n";

			final JLabel panic_l = new JLabel(panic);
			String injured = "injured : ";
			if (agent.injured)
				injured += "yes \n";
			else
				injured += "no \n";
			final JLabel inj = new JLabel(injured);

			final Box b = Box.createVerticalBox();

			b.add(Box.createGlue());
			b.add(age_range);

			b.add(Box.createGlue());
			b.add(gender);

			b.add(Box.createGlue());
			b.add(mob_r);

			b.add(Box.createGlue());
			b.add(healt_l);

			b.add(Box.createGlue());
			b.add(panic_l);

			b.add(Box.createGlue());
			b.add(inj);

			b.add(Box.createGlue());
			getContentPane().add(b, "Center");

			JPanel p2 = new JPanel();
			JButton ok = new JButton("Ok");
			p2.add(ok);
			getContentPane().add(p2, "South");

			ok.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					setVisible(false);
					b.remove(age_range);
					b.remove(panic_l);
					b.remove(healt_l);
					b.remove(gender);
					b.remove(mob_r);
				}
			});

			setSize(250, 150);
		}
	}

	public SimpleAboutDialog getDialog() {
		return this.dialog;
	}

}